const EventEmitter = require('events').EventEmitter
const fs = require('fs')
const path = require('path')
const emitter = new EventEmitter()

fs.readdirSync(__dirname).forEach((n, i) => {
  if (n === 'index.js') return
  let eventName = n

  fs.readdirSync(path.join(__dirname, eventName)).forEach((nn, ii) => {
    let listenerName = nn.replace(/\.js$/, '')
    let listener = require(`./${eventName}/${listenerName}`)
    if (!listener.enable) return
    emitter.on(eventName, (data) => {
      console.info('执行事件', '---', listenerName, '---', listener.title || '', '---', new Date())
      listener.run(data)
    })
  })
})

module.exports = emitter
